//
//  EncryptUtil.m
//  text
//
//  Created by lanxum on 16/9/27.
//  Copyright © 2016年 lanxum. All rights reserved.
//

#import "EncryptUtil.h"
#import <CommonCrypto/CommonCryptor.h>
#import "GTMBase64.h"

#import <CommonCrypto/CommonDigest.h>

#define gkey            @"LmMGStGtOpF4xNyvYt54EQ=="

@implementation EncryptUtil

+ (NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key
{

    NSData* plainTextdata = [plainText dataUsingEncoding:NSUTF8StringEncoding];
    NSUInteger plainTextdatLength = [plainTextdata length];
    unsigned char digest[16];
    CC_MD5([plainTextdata bytes],(CC_LONG) plainTextdatLength, digest);
    NSUInteger plainTextBufferTotalSize  = 16 +plainTextdatLength;
    Byte *testByte = (Byte *)[plainTextdata bytes];
    Byte totalByte[plainTextBufferTotalSize];
    for (int i = 0; i < plainTextBufferTotalSize; ++i) {
        if (i<16) {
            totalByte[i] =digest[i];
        }else{
            totalByte[i] =testByte[i - 16];
        }
    }
    
    NSData *baseKey = [GTMBase64 decodeString:key];
    Byte *buf = (Byte *)[baseKey bytes];
    Byte key1[8];
    Byte iv2[8];
    for (int i = 0; i < 8; i++) {
        key1[i] = buf[i];
    }
    for (int i = 0; i < 8 ; i++) {
        iv2[i] = buf[i + 8];
    }
    
    NSString *ciphertext = nil;
    unsigned char buffer[1024];
    memset(buffer, 0, sizeof(char));
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
                                          kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding,
                                          key1,
                                          kCCKeySizeDES,
                                          iv2,
                                          totalByte,
                                          plainTextBufferTotalSize,
                                          buffer,
                                          1024,
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        
        NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
        ciphertext = [[NSString alloc] initWithData:[GTMBase64 encodeData:data] encoding:NSUTF8StringEncoding];
    }
    return ciphertext;
}


+ (NSString *) decryptUseDES:(NSString *)plainText key:(NSString *)key {
    NSData *BasePlainTextData = [GTMBase64 decodeString:plainText];
    Byte *BasePlainTextDataByte = (Byte *)[BasePlainTextData bytes];
    
    NSData *baseKey = [GTMBase64 decodeString:key];
    Byte *buf = (Byte *)[baseKey bytes];
    Byte keyByte[8];
    Byte ivByte[8];
    for (int i = 0; i < 8; i++) {
        keyByte[i] = buf[i];
    }
    for (int i = 0; i < 8 ; i++) {
        ivByte[i] = buf[i + 8];
    }
    
    size_t bufferSize = BasePlainTextData.length;
    unsigned char buffer[1024];
    memset(buffer,0,sizeof(char));
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
                                          kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding,
                                          keyByte,
                                          kCCKeySizeDES,
                                          ivByte,
                                          BasePlainTextDataByte,
                                          bufferSize,
                                          buffer,
                                          1024,
                                          &numBytesEncrypted);
    
    
    NSData *resultdata;
    if (cryptStatus == kCCSuccess) {
        
        resultdata = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
    }
    Byte *resultByte = (Byte *)[resultdata bytes];
    size_t returnLength = resultdata.length - 16;
    /// 定义
    Byte decryptionByte[returnLength];
    for (int i = 0; i < returnLength; ++i) {
        decryptionByte[i] = resultByte[i+16];
    }
    NSData *namedata = [[NSData alloc] initWithBytes:decryptionByte length:returnLength];
    NSString *str = [[NSString alloc] initWithData:namedata encoding:NSUTF8StringEncoding];
    return str;
}

+ (NSString*)dictionaryToJson:(NSMutableDictionary *)dic {
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
}

+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
    
}


+(NSString *)getDataStringWithLength:(int)length with :(unsigned char*)digest{
    NSMutableString *output = [NSMutableString stringWithCapacity:length * 2];
    unsigned char byte[length];
    for(int i = 0; i < length; i++){
        byte[i] =(char)digest[i];
        [ output appendFormat:@"%d",(char)byte[i]];
        
    }
    return output.copy;
}


@end
